﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLVatTu.Class;
using QLVatTu.InPhieu;

namespace QLVatTu.NghiepVu
{
    public partial class frmQuanLyXuatThanhLyKhachHang : Form
    {
        public frmQuanLyXuatThanhLyKhachHang()
        {
            InitializeComponent();
        }

        KetNoiDB cn = new KetNoiDB();

        private void btnThem_Click(object sender, EventArgs e)
        {
            frmThemPhieuXuatKhachHang frm = new frmThemPhieuXuatKhachHang();
            frm.ShowDialog();
            load_dtgPhieuXuat();
        }
        
        private void load_dtgPhieuXuat(string dieuKien=null)
        {
            string sql = "";
            if (dieuKien != null)
            {
                sql = @"SELECT     dbo.tbl_PhieuXuatKho.SoPhieu, dbo.tbl_PhieuXuatKho.NgayXuat, dbo.tbl_PhieuXuatKho.MaDoiTac, dbo.tbl_DoiTac.TenDoiTac, dbo.tbl_PhieuXuatKho.MaKho, 
                                              dbo.tbl_Kho.TenKho, dbo.tbl_PhieuXuatKho.NguoiXuat, dbo.tbl_PhieuXuatKho.DienGiai, XacNhan = case XacNhan when '0' then N'Chưa xác nhận' when '1' then N'Đã xác nhận' end
                        FROM         dbo.tbl_PhieuXuatKho INNER JOIN
                                              dbo.tbl_Kho ON dbo.tbl_PhieuXuatKho.MaKho = dbo.tbl_Kho.MaKho INNER JOIN
                                              dbo.tbl_DoiTac ON dbo.tbl_PhieuXuatKho.MaDoiTac = dbo.tbl_DoiTac.MaDoiTac INNER JOIN
                                              dbo.tbl_LoaiPhieu ON dbo.tbl_PhieuXuatKho.MaLoaiPhieu = dbo.tbl_LoaiPhieu.MaLoaiPhieu AND 
                                              dbo.tbl_PhieuXuatKho.MaLoaiPhieu = dbo.tbl_LoaiPhieu.MaLoaiPhieu
                        "
                        + dieuKien;
            }
            else
            {
                sql = @"
                        SELECT     dbo.tbl_PhieuXuatKho.SoPhieu, dbo.tbl_PhieuXuatKho.NgayXuat, dbo.tbl_PhieuXuatKho.MaDoiTac, dbo.tbl_DoiTac.TenDoiTac, dbo.tbl_PhieuXuatKho.MaKho, 
                                              dbo.tbl_Kho.TenKho, dbo.tbl_PhieuXuatKho.NguoiXuat, dbo.tbl_PhieuXuatKho.DienGiai, XacNhan = case XacNhan when '0' then N'Chưa xác nhận' when '1' then N'Đã xác nhận' end
                        FROM         dbo.tbl_PhieuXuatKho INNER JOIN
                                              dbo.tbl_Kho ON dbo.tbl_PhieuXuatKho.MaKho = dbo.tbl_Kho.MaKho INNER JOIN
                                              dbo.tbl_DoiTac ON dbo.tbl_PhieuXuatKho.MaDoiTac = dbo.tbl_DoiTac.MaDoiTac INNER JOIN
                                              dbo.tbl_LoaiPhieu ON dbo.tbl_PhieuXuatKho.MaLoaiPhieu = dbo.tbl_LoaiPhieu.MaLoaiPhieu AND
                                              dbo.tbl_PhieuXuatKho.MaLoaiPhieu = dbo.tbl_LoaiPhieu.MaLoaiPhieu 
                        WHERE     dbo.tbl_PhieuXuatKho.MaLoaiPhieu = 'LP005'";
            }
            DataTable tbl = cn.getTable(sql);
            dtgPhieuXuatThanhLy.DataSource = tbl;
        }

        private void loadData()
        {
            cbbKhoXuat.DataSource = cn.getTable("select * from tbl_Kho");
            cbbKhoXuat.DisplayMember = "TenKho";
            cbbKhoXuat.ValueMember = "MaKho";

            cbbKhachHang.DataSource = cn.getTable("select * from tbl_DoiTac where KhachHang=1");
            cbbKhachHang.DisplayMember = "TenDoiTac";
            cbbKhachHang.ValueMember = "MaDoiTac";

            load_dtgPhieuXuat();
        }

        private void frmQuanLyXuatThanhLyKhachHang_Load(object sender, EventArgs e)
        {
            if (cn.KetNoi())
            {
                loadData();
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (TopLevel == false) this.Parent.Parent.Controls.Remove(this.Parent);
            else this.Close();
        }

        private void dtgChiTiet_SelectionChanged(object sender, EventArgs e)
        {
           
        }

        private void dtgPhieuXuat_SelectionChanged(object sender, EventArgs e)
        {
            if (dtgPhieuXuatThanhLy.SelectedRows.Count > 0)
            {
                if (cn.KetNoi())
                {
                    string sql = @"SELECT row_number() over(order by tbl_ChiTietPhieuXuat.MaChiTietPhieuXuat) as STT,tbl_ChiTietPhieuXuat.MaVatTu, tbl_VatTu.TenVatTu, 
                                    tbl_DonViTinh.TenDonViTinh, tbl_ChiTietPhieuXuat.SoLuongPhePham 
                                 FROM tbl_ChiTietPhieuXuat INNER JOIN 
                                      tbl_DonViTinh ON tbl_ChiTietPhieuXuat.MaDonViTinh = tbl_DonViTinh.MaDonViTinh INNER JOIN 
                                      tbl_VatTu ON tbl_ChiTietPhieuXuat.MaVatTu = tbl_VatTu.MaVatTu 
                                 WHERE tbl_ChiTietPhieuXuat.SoPhieu = '" + dtgPhieuXuatThanhLy.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'";
                    DataTable tbl = cn.getTable(sql);
                    dtgChiTiet.DataSource = tbl;

                    sql = "SELECT [XacNhan] FROM [tbl_PhieuXuatKho] WHERE [SoPhieu] = '" + dtgPhieuXuatThanhLy.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'";
                    bool _XacNhan = bool.Parse(cn.LayDuLieu(sql));
                    btnXacNhan.Enabled = !_XacNhan;
                }
            }
        }

        public static string SoPhieu = "";

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (dtgPhieuXuatThanhLy.SelectedRows.Count > 0 && dtgPhieuXuatThanhLy.CurrentRow.Cells["colSoPhieu"].Value != null)
            {
                string sql = "SELECT [XacNhan] FROM [tbl_PhieuXuatKho] WHERE [SoPhieu] = '" + dtgPhieuXuatThanhLy.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'";
                bool _XacNhan = bool.Parse(cn.LayDuLieu(sql));
                if (_XacNhan == true)
                {
                    MessageBox.Show("Bạn không thể sửa phiếu đã xác nhận", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                frmThemPhieuXuatKhachHang frm = new frmThemPhieuXuatKhachHang();
                frm.MaPhieuXuat=dtgPhieuXuatThanhLy.CurrentRow.Cells["colSoPhieu"].Value.ToString();
                frm.ShowDialog();
                load_dtgPhieuXuat();
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dtgPhieuXuatThanhLy.RowCount == 0) return;
            string sql = "SELECT [XacNhan] FROM [tbl_PhieuXuatKho] WHERE [SoPhieu] = '" + dtgPhieuXuatThanhLy.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'";
            bool _XacNhan = bool.Parse(cn.LayDuLieu(sql));
            if (_XacNhan == true)
            {
                MessageBox.Show("Bạn không thể xóa phiếu đã xác nhận.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            DialogResult msg = MessageBox.Show("Bạn chắc chắn muốn xóa?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (msg == DialogResult.Yes)
            {
                cn.Delete_Table("tbl_ChiTietPhieuXuat", "SoPhieu='" + dtgPhieuXuatThanhLy.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'");
                cn.Delete_Table("tbl_LuuKho", "SoPhieu='" + dtgPhieuXuatThanhLy.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'");
                cn.Delete_Table("tbl_PhieuXuatKho", "SoPhieu='" + dtgPhieuXuatThanhLy.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'");
                MessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("Xóa không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            load_dtgPhieuXuat();
        }

        private void ckbSoPhieu_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbSoPhieu.Checked == true)
            {
                txtSoPhieu.Text = "";
                txtSoPhieu.Enabled = true;
            }
            else
            {
                txtSoPhieu.Text = "Tìm theo số phiếu";
                txtSoPhieu.Enabled = false;
            }
        }

        private void ckbKhoXuat_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbKhoXuat.Checked == true)
            {
                cbbKhoXuat.Enabled = true;
            }
            else
            {
                cbbKhoXuat.Enabled = false;
            }
        }

        private void ckbKhacHang_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbKhacHang.Checked == true)
            {
                cbbKhachHang.Enabled = true;
            }
            else
            {
                cbbKhachHang.Enabled = false;
            }
        }

        private void ckbNguoiLap_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbNguoiLap.Checked == true)
            {
                txtNguoiXuat.Text = "";
                txtNguoiXuat.Enabled = true;
            }
            else
            {
                txtNguoiXuat.Text = "Tìm theo người lập phiếu";
                txtNguoiXuat.Enabled = false;
            }
        }

        private string XacDinhDieuKienLoc()
        {
            string dieuKienLoc = "where 1=1 and tbl_PhieuXuatKho.MaLoaiPhieu='LP005'";

            if (ckbSoPhieu.Checked == true && txtSoPhieu.Text.Trim() != "")
            {
                dieuKienLoc += "and tbl_PhieuXuatKho.MaLoaiPhieu like '%"+txtSoPhieu.Text.Trim()+"%'";
            }

            if (ckbKhoXuat.Checked == true && cbbKhoXuat.Items.Count > 0)
            {
                dieuKienLoc += "and tbl_PhieuXuatKho.MaKho='"+cbbKhoXuat.SelectedValue.ToString()+"'";
            }

            if (ckbKhacHang.Checked == true && cbbKhachHang.Items.Count > 0)
            {
                dieuKienLoc += "and tbl_PhieuXuatKho.MaDoiTac='"+cbbKhachHang.SelectedValue.ToString()+"'";
            }

            if (ckbNguoiLap.Checked == true && txtNguoiXuat.Text.Trim() != "")
            {
                dieuKienLoc += "and tbl_PhieuXuatKho.NguoiXuat like '%"+txtNguoiXuat.Text.Trim()+"%'";
            }

            if (ckbNgayXuat.Checked == true)
            {
                dieuKienLoc += "and tbl_PhieuXuatKho.NgayXuat between '"+dtipTuNgay.Value.ToString()+"' and '"+dtipDenNgay.Value.ToString()+"'";
            }

            return dieuKienLoc;
        }

        private void ckbNgayXuat_CheckedChanged(object sender, EventArgs e)
        {
            dtipTuNgay.Enabled = ckbNgayXuat.Checked;
            dtipDenNgay.Enabled = ckbNgayXuat.Checked;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            frmInPhieuXuatThanhLy frm = new frmInPhieuXuatThanhLy();
            if (dtgPhieuXuatThanhLy.SelectedRows.Count > 0 && dtgPhieuXuatThanhLy.CurrentRow.Cells["colSoPhieu"].Value != null)
            {
                string SoPhieu = dtgPhieuXuatThanhLy.CurrentRow.Cells["colSoPhieu"].Value.ToString();
                string sql = @"SELECT     dbo.tbl_PhieuXuatKho.SoPhieu, dbo.tbl_PhieuXuatKho.NgayXuat, dbo.tbl_PhieuXuatKho.MaDoiTac, dbo.tbl_DoiTac.TenDoiTac, dbo.tbl_PhieuXuatKho.MaKho, 
                                              dbo.tbl_Kho.TenKho, dbo.tbl_PhieuXuatKho.NguoiXuat, dbo.tbl_PhieuXuatKho.DienGiai, dbo.tbl_PhieuXuatKho.MaLoaiPhieu, dbo.tbl_LoaiPhieu.TenLoaiPhieu
                        FROM         dbo.tbl_PhieuXuatKho INNER JOIN
                                              dbo.tbl_Kho ON dbo.tbl_PhieuXuatKho.MaKho = dbo.tbl_Kho.MaKho INNER JOIN
                                              dbo.tbl_DoiTac ON dbo.tbl_PhieuXuatKho.MaDoiTac = dbo.tbl_DoiTac.MaDoiTac INNER JOIN
                                              dbo.tbl_LoaiPhieu ON dbo.tbl_PhieuXuatKho.MaLoaiPhieu = dbo.tbl_LoaiPhieu.MaLoaiPhieu AND
                                              dbo.tbl_PhieuXuatKho.MaLoaiPhieu = dbo.tbl_LoaiPhieu.MaLoaiPhieu 
                        WHERE     dbo.tbl_PhieuXuatKho.SoPhieu = '"+SoPhieu+"'";
                DataTable tbl = cn.getTable(sql);
                if (tbl.Rows.Count > 0)
                {
                    List<PhieuXuatThanhLy> list = new List<PhieuXuatThanhLy>();
                    DateTime dtNgayXuat=DateTime.Parse(tbl.Rows[0]["NgayXuat"].ToString());
                    string Ngay = dtNgayXuat.Day.ToString();
                    string Thang = dtNgayXuat.Month.ToString();
                    string Nam = dtNgayXuat.Year.ToString();
                    string TenDoiTac=tbl.Rows[0]["TenDoiTac"].ToString();
                    string TenKho=tbl.Rows[0]["TenKho"].ToString();
                    string NguoiXuat=tbl.Rows[0]["NguoiXuat"].ToString();
                    string DienGiai=tbl.Rows[0]["DienGiai"].ToString();
                    string TenLoaiPhieu=tbl.Rows[0]["TenLoaiPhieu"].ToString();

                    sql = @"SELECT row_number() over(order by tbl_ChiTietPhieuXuat.MaChiTietPhieuXuat) as STT, tbl_VatTu.MaVatTu, tbl_VatTu.TenVatTu, 
                                    tbl_DonViTinh.TenDonViTinh, tbl_ChiTietPhieuXuat.SoLuongPhePham 
                                 FROM tbl_ChiTietPhieuXuat INNER JOIN 
                                      tbl_DonViTinh ON tbl_ChiTietPhieuXuat.MaDonViTinh = tbl_DonViTinh.MaDonViTinh INNER JOIN 
                                      tbl_VatTu ON tbl_ChiTietPhieuXuat.MaVatTu = tbl_VatTu.MaVatTu 
                                 WHERE tbl_ChiTietPhieuXuat.SoPhieu='"+SoPhieu+"'";
                    DataTable dt = cn.getTable(sql);
                    if (dt.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            PhieuXuatThanhLy phieu = new PhieuXuatThanhLy();
                            phieu.SoPhieu = SoPhieu;
                            phieu.Ngay = Ngay;
                            phieu.Thang = Thang;
                            phieu.Nam = Nam;
                            phieu.NguoiXuatHang = NguoiXuat;
                            phieu.KhachHang = TenDoiTac;
                            phieu.DienGiai = DienGiai;
                            phieu.Kho = TenKho;
                            phieu.HangMuc = TenLoaiPhieu;

                            phieu.STT=dr["STT"].ToString();
                            phieu.MaVT=dr["MaVatTu"].ToString();
                            phieu.TenVatTu=dr["TenVatTu"].ToString();
                            phieu.DVT=dr["TenDonViTinh"].ToString();
                            phieu.SL=dr["SoLuongPhePham"].ToString();
                            list.Add(phieu);
                        }

                    }
                    frm.phieuXuatThanhLyBindingSource.DataSource = list;
                    frm.ShowDialog();
                }
            }
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            load_dtgPhieuXuat(XacDinhDieuKienLoc());
        }

        private void radLoad_CheckedChanged(object sender, EventArgs e)
        {
            radLoad.Checked = false;
            load_dtgPhieuXuat();
            ckbKhacHang.Checked = false;
            ckbKhoXuat.Checked = false;
            ckbNgayXuat.Checked = false;
            ckbNguoiLap.Checked = false;
            ckbSoPhieu.Checked = false;
        }

        private void btnXacNhan_Click(object sender, EventArgs e)
        {
            if (dtgPhieuXuatThanhLy.SelectedRows.Count > 0 && dtgPhieuXuatThanhLy.CurrentRow.Cells["colSoPhieu"].Value != null)
            {
                DialogResult msg = MessageBox.Show("Sau khi xác nhận bạn không thể sửa và xóa phiếu!", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (msg == DialogResult.Yes)
                {
                    cn.Update_Table("tbl_PhieuXuatKho", "XacNhan=1", "SoPhieu='" + dtgPhieuXuatThanhLy.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'");
                    foreach (DataGridViewRow dgvr in dtgChiTiet.Rows)
                    {
                        if (dgvr.IsNewRow) continue;
                        string sql = "SELECT MaKho FROM dbo.tbl_PhieuXuatKho WHERE SoPhieu = '" + dtgPhieuXuatThanhLy.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'";
                        string MaKho = cn.LayDuLieu(sql);
                        string MaVatTu = dgvr.Cells["colMaVatTu"].Value.ToString();
                        string SoPhieu = dtgPhieuXuatThanhLy.CurrentRow.Cells["colSoPhieu"].Value.ToString();
                        string NgayLapPhieu = dtgPhieuXuatThanhLy.CurrentRow.Cells["colNgayXuat"].Value.ToString();

                        float SoLuongThanhLy = float.Parse(dgvr.Cells["colSoLuong"].Value.ToString());
                        float SoLuongPhePham = -SoLuongThanhLy;
                        cn.Insert_Table("tbl_LuuKho", "SoPhieu,MaKho,MaVatTu,SoLuong,SoLuongPhePham,NgayCapNhat", "'" + SoPhieu + "','" + MaKho + "','" + MaVatTu + "',0,'" + SoLuongPhePham + "','" + NgayLapPhieu + "'");

                    }
                    load_dtgPhieuXuat();
                }
            }
        }
    }
}
